//
//  MKReaderNavigationView.swift
//  Fanmugua
//
//  Created by MK on 2021/3/30.
//  Copyright © 2021 taolang. All rights reserved.
//

import UIKit

@objc protocol MKReaderNavigationViewDelegate:NSObjectProtocol {
    
    @objc func backAction()
    @objc func shareAction()
    @objc func likeAction(button:UIButton)
    @objc func reportAction()
    @objc func moreBtnAction(isShow:Bool)
 
}

class MKReaderNavigationView: UIView {

    private var labTitle:UILabel!
    private var btnBack:UIButton!
    private var btnLike:UIButton!
    private var btnShare:UIButton!
    private var btnReport:UIButton!
    private var btnMore:UIButton!
    private var isShow:Bool! = true
    @objc var isShowTool:Bool = false {
        didSet {
            
        }
    }
    @objc weak var delegate:MKReaderNavigationViewDelegate!

//    var btnMoreCompletion:VoidBlock?
    
    @objc var title:String! {
        didSet{
            labTitle.text = title
        }
    }
    
    @objc var isCollect: Bool = false {
        didSet {
            btnLike.isSelected = isCollect
        }
    }
    
    @objc class func createView() -> MKReaderNavigationView {
        return MKReaderNavigationView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: NAVGATION_VIEW_HEIGHT));
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        setupSubView()
        refreshStyleUI()
    }
    
    private func setupSubView(){
        
        btnBack = UIButton(type: .custom)
        btnBack.frame = CGRect(x: 6, y: STATUS_BAR_HEIGHT, width: 44, height: 44)
        btnBack.addTarget(self, action:#selector(btnBackAction), for: .touchUpInside)
        addSubview(btnBack)
        
        labTitle = UILabel(frame: CGRect(x: 50, y: STATUS_BAR_HEIGHT, width: SCREEN_WIDTH, height: 44))
        labTitle.textAlignment = .left
        labTitle.font = .systemFont(ofSize: 17, weight: .medium)
        addSubview(labTitle)
        
        btnLike = UIButton(type: .custom)
        btnLike.frame = CGRect(x: SCREEN_WIDTH-12-44-44, y:STATUS_BAR_HEIGHT, width: 44, height: 44)
        btnLike.addTarget(self, action: #selector(btnLikeAction(_:)), for: .touchUpInside)
        btnLike.setTitle("收藏", for: .normal)
        btnLike.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
        btnLike.setTitleColor(.init(hexRGB: 0x848484), for: .normal)
        btnLike.isHidden = true
        addSubview(btnLike)
        
        btnMore = UIButton(type: .custom)
        btnMore.frame = CGRect(x: SCREEN_WIDTH-12-44, y: STATUS_BAR_HEIGHT, width: 44, height: 44)
        btnMore.addTarget(self, action: #selector(btnShowToolViewAction), for: .touchUpInside)
        btnMore.setTitle("更多", for: .normal)
        btnMore.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
        btnMore.setTitleColor(.init(hexRGB: 0x848484), for: .normal)
        btnMore.isHidden = true
        addSubview(btnMore)
        
//        btnReport = UIButton(type: .custom)
//        btnReport.frame = CGRect(x: SCREEN_WIDTH-12-44-44-44, y:STATUS_BAR_HEIGHT, width:44, height: 44)
//        btnReport.addTarget(self, action: #selector(btnReportAction), for: .touchUpInside)
//        addSubview(btnReport)
        
    }
    
    @objc private func btnShowToolViewAction(){
        isShowTool = !isShowTool
        delegate?.moreBtnAction(isShow: isShowTool)
    }
  
    @objc func refreshStyleUI(){
        
        backgroundColor = MKReaderCfg.shared().bgColor
        
        if MKReaderCfg.shared().bgColorIndex == 0{
            
            labTitle.textColor = .black
            
            btnBack.setImage(UIImage(named:"tzxqy_fh"), for: .normal)
        }else{
            
            labTitle.textColor = .white
            
            btnBack.setImage(UIImage(named:"ydy_fh"), for: .normal)
        }
        
                
//        btnBack.setImage(UIImage(named:"tzxqy_fh"), for: .normal)
//        btnShare.setImage(UIImage(named:"tn_btn_share"), for: .normal)
//        btnReport.setImage(UIImage(named: "tn_section_report"), for: .normal)
        btnLike.setImage(UIImage(named:"xsydy_sc2"), for: .normal)
        btnLike.setImage(UIImage(named:"xsydy_sc"), for: .selected)
        btnMore.setImage(UIImage(named: "xsydy_gd"), for: .normal)
//        btnLike.setImageAboveText(spacing: 3)
//        btnMore.setImageAboveText(spacing: 3)
    }
    
    @objc private func btnBackAction(){
        delegate?.backAction()
    }
    
    @objc private func btnLikeAction(_ item:UIButton!){
        delegate?.likeAction(button: item)
    }
    
    @objc private func btnShareAction(){
        delegate?.shareAction()
    }
    
    @objc private func btnReportAction(){
        delegate?.reportAction()
    }
    
    
    @objc func showView(show:Bool){
        if show == isShow{
            return
        }
        UIView.animate(withDuration: MKREADER_ANIMATE_DURATION,
                       delay: 0,
                       options: .curveEaseOut) {[weak self] () in
            let y = show ? 0 : -NAVGATION_VIEW_HEIGHT
            self?.frame.origin = CGPoint(x: 0, y: y)
        } completion: { [weak self] (ok) in
            self?.isShow = show
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
